#pragma once

#include "explorer_window.h"
#include "MySQLEx.h"


class CAppConfig
{
public:
	CAppConfig(void);
	~CAppConfig(void);

	inline void SetINIPath(CString path){ m_INIPath = path; }
	inline CString GetINIPath(void){ return m_INIPath; }

	INT ReadConfig(void);
	INT WriteConfig(void);

	BOOL ShowLastError(DWORD error);
	BOOL MapDrive(LPCTSTR szPath, BOOL bConnect = FALSE, LPCTSTR szDomain =_T(""), LPCTSTR szUser =_T(""), LPCTSTR szPass =_T(""));
	CString GetFormattedDate(INT nProject = 0);
	CString GetFormattedDateTime(BOOL bDateOnly = FALSE, BOOL bTabbed = TRUE, BOOL bNonFile = FALSE);
	BOOL FormatMacros(CString* pszString, LONG nItemID, LONG nCurrentStaffID=-1, LONG nNewStatusID=-1, LONG nCurrentLibraryID=-1, LONG nUserID=-1, CFilePair* pFiles=NULL);

	LONG GetIDFromString(CString szString);
	LONG FindStringArrayID(CStringArray* pStringArray, CString szString, CString szType);

	BOOL WriteWindowLocation(BOOL bReset = FALSE);
	BOOL WriteWindowLocation(CString szWindow, CRect rect);
	BOOL ReadWindowLocation(void);
	BOOL ReadWindowLocation(CString szWindow, CRect* prect);

	COLORREF GetColorRef(CString szColor);
	COLORREF DarkenColor(COLORREF clrColor, DOUBLE darken);
	COLORREF LightenColor(COLORREF clrColor, DOUBLE lighten);

	UINT Convert2Ascii(UINT nChar);
	BOOL NormalizeString(CString* pszString);
	BOOL CorrectCitation(CString* pszCitation);
	CString QuoteString(CString szString);
	BOOL Convert2Multibyte(CString* pszString, char* pszDest, INT nDestLen);
	INT SplitString(const CString& strInputString, const CString& strDelimiter, CStringArray& arrStringArray);

	BOOL UpdateRequestHistory(LONG nItemID, LONG nUserID, LONG nStatus, CString szText);
	BOOL UpdateUserHistory(LONG nUserID, CString szText);

	void WriteToLog(CString filename, CString text, CString token, CString number, CString title);
	void WriteToFile(CString filename, CString text, BOOL bRemoveReturns = FALSE);
	void EnCrypt(TCHAR* inp, TCHAR* key = _T(""));
	BOOL Hash(CString* pszString);

	HRESULT GetHTMLBody(CExplorer_window* pBrowser, CString &szHTML);
	HRESULT GetHTML(CExplorer_window* pBrowser, CString &szHTML);
	HRESULT SetHTML(CExplorer_window* pBrowser, CString szHTML);
	HRESULT ClearHTML(CExplorer_window* pBrowser);
	HRESULT SetEdit(CExplorer_window* pBrowser, BOOL bEdit = TRUE);
	HRESULT SetBold(CExplorer_window* pBrowser, BOOL bState = TRUE);
	HRESULT SetFontName(CExplorer_window* pBrowser);
	HRESULT SetFontSize(CExplorer_window* pBrowser, INT nSize);
	HRESULT SetItalic(CExplorer_window* pBrowser, BOOL bState = TRUE);
	HRESULT SetColor(CExplorer_window* pBrowser, COLORREF cColor);
	HRESULT LoadFromFolder(CExplorer_window* pBrowser, CString szResource);
	HRESULT LoadFromResource(CExplorer_window* pBrowser, UINT nRes);
	HRESULT SetMargins(BOOL bReset = FALSE);

	CString ConvertHTMLToPlainText(const CString& sHtml);

	CString ReadFile(CString szFile);
	CString GetIDFromRequest(CString szCitation);
	BOOL PrintRequest(CDC* pdc, LONG nItem, INT nCount);
	INT PrintLine(CDC* pdc, CString szLine);
	BOOL GetLDAPStaff(CString szServer, CString szUser, CString szPass, LDAP_STAFF_INFO* pStaffInfo);
	BOOL GetLDAPUser(CString szUser, LDAP_USER_INFO* pUserInfo, BOOL bCheck=TRUE);

	INT_PTR GetHelpLinks(CStringArray &csaHelpLinks);

private:
	void Config(BOOL bRead);

	CString ReadINIString(CString Section, CString Item, CString Default);
	INT ReadINIInt(CString Section, CString Item, INT Default);
	LONG ReadINILong(CString Section, CString Item, LONG Default);
	BOOL ReadINIBool(CString Section, CString Item);
	BOOL ReadINIBool(CString Section, CString Item, BOOL Default);
	INT_PTR ReadINISectionKeys(CStringArray &csaKeys, CString Section);

	BOOL WriteINIBool(CString Section, CString Item, BOOL Value);
	BOOL WriteINIString(CString Section, CString Item, CString Value);
	BOOL WriteINIInt(CString Section, CString Item, INT Default);
	BOOL WriteINILong(CString Section, CString Item, LONG Default);

	void ReadRegString(CString Section, CString Item, CString Default, CString* Value);
	void ReadRegInt(CString Section, CString Item, LPINT Value);
	void ReadRegLong(CString Section, CString Item, LPLONG Value);
	void ReadRegBool(CString Section, CString Item, LPBOOL Value);

	void ModifyINIString(BOOL bRead, CString Section, CString Item, CString Default, CString* Value);
	void ModifyINIInt(BOOL bRead, CString Section, CString Item, INT Default, LPINT Value);
	void ModifyINILong(BOOL bRead, CString Section, CString Item, LONG Default, LPLONG Value);
	void ModifyINIBool(BOOL bRead, CString Section, CString Item, BOOL Default, LPBOOL Value);

public:
	CString m_LicenseType;
	BOOL m_Registered;
	INT m_nStaffLevel;
	CMySQLEx m_SQL;

private:
	LONG m_nPrintY;
	CString m_INIPath;
	CString m_ScFile;
	CTime m_LicenseDate;
	LONG m_nDaysLeft;
	CStringArray m_csaACList;

public:
	// [General]
	struct GENERAL
	{
		CString szLocation;
		CString szRootFolder;
		CString szDirectURL;
		CString szRootWebPHP;
        CString szRootWebAdmin;
		CString szUserSite;
		CString szWEBLink;
		CString szWEBNALink;
		CString szFTPLink;
		CString szLookupLink;
		CString szSourceLink;
		CString szSendHtmlFileLink;
		CString szWorkerLink;
		CString szPUBMEDLink;
		CString szStatusIconPath;
		BOOL bRemoveFileAfterSend;
		CString szWebAttachFolder;
		BOOL bWriteHistory;
		CString szHistoryFile;
		CString szInformationFile;
		CString szHoldDateFile;
		CString szInformationFileSQL;
		CString szEnvironmentVariable;
		BOOL bUseUniqueHistoryFiles;
		INT nDeleteFilesOverDays;
		INT nAdvertiseDaysOld;
		CString szAdvertiseDateFormat;
		BOOL bRemoveEmptyFolders;
		BOOL bEnableDeleteHistory;
		BOOL bEnableOpenHistory;
		CString szFileDescriptionPrefix;
		CString szDefaultDescription;
		CString szRequesterInfoFile;
		CString szRequestNoteFile;
		CString szRequestFile;
		CString szSendInfoFile;
		INT nStartHeight;
		INT nStartWidth;
		BOOL bStartWebView;
		CString szDateFormat;
		CString szShortDateFormat;
		BOOL bStoreFilesInDataBase;
		BOOL bSendFilesViaHTTP;
		BOOL bSendFilesViaFTP;
		CString szFileTypesFormat;
		BOOL bFileCopyNoUI;
		INT nDefaultDaysSearch;
		INT nDaysShownComplete;
		INT nHoursClaimLimit;
		CString szPasswordHash;
		BOOL bTestSystem;
		BOOL bUseSitckyCollection;
		BOOL bUseAutoLinkSourcing;
		BOOL bUseStickyNAReason;
		CString szRequestAddFile;
	} m_General;

	//[LDAP]
	struct LDAP
	{
		BOOL bUseLDAPLogons;
		BOOL bAutoAddStaff;
		CString szServer;
		CString szUserName;
		CString szPassword;
	} m_LDAP;

	//[Printing]
	struct PRINTING
	{
		INT nRequestsPerPage;
		CString szRequestTemplate;
		CString szTemplatePageBreak;
		BOOL bPrintPreview;
		CString szHeader;
		CString szFooter;
		CString szMarginBottom;
		CString szMarginLeft;
		CString szMarginRight;
		CString szMarginTop;
	} m_Printing;

	//[Options]
	struct OPTIONS
	{
		BOOL bDisableMyView;
		BOOL bDisableILL;
		BOOL bDisableAtLibrary;
		BOOL bUseComapnyForLocation;
		BOOL bDisableFinacial;
		BOOL bDisableReferrals;
		BOOL bDisableCopyright;
		BOOL bDisableProject;
		BOOL bDisableFileSendExternally;
		BOOL bDisableTriageProcess;
		BOOL bDisableSourceLinkCheck;
		BOOL bDisableCombineNASendReturn;
	} m_Options;

	// [SFX]
	struct SFX
	{
		CString szTitleStartSearch;
		CString szTitleMidSearch;
		CString szTitleEndSearch;
		CString szSourceStartSearch;
		CString szSourceMidSearch;
		CString szSourceEndSearch;
		CString szSourceEndString;
	} m_SFX;

	// [Colors]
	struct COLORS
	{
		DWORD dwTriageHighlight;
		DWORD dwSortHighlight;
		DWORD dwDuplicateHighlight;
		DWORD dwMaterialLicensed;
		DWORD dwMaterialFree;
		DWORD dwMaterialNotFree;
		DWORD dwLibraryListed;
	} m_Colors;

	// [Database]
	struct DATABASE
	{
		CString szSourceName;
		CString szDriver;
		CString szDatabase;
		CString szServer;
		CString szPort;
		CString szUID;
		CString szPWD;
		INT nLimit;
		INT nRange;
		INT nRefreshTime;
		BOOL bUpdateOnLogin;
	} m_Database;

	// [SecondaryDatabase]
	struct EXTERNAL
	{
		CString szDatabase;
		CString szServer;
		CString szPort;
		CString szUID;
		CString szPWD;
		CString szFTPServer;
		CString szFTPFolder;
		CString szFTPUID;
		CString szFTPPWD;
		CString szWEBLink;
		CString szWEBNALink;
		CString szUserSite;
	} m_External;

	// [Queue]
	struct QUEUE
	{
		BOOL bEnableQueue;
		CString szQueueFolder;
		BOOL bShowQueueErrors;
		CString szQueueErrorEmail;
		BOOL bShowCopyDialog;
	} m_Queue;

	// [Email]
	struct EMAIL
	{
		CString szContactInfo;
		CString szRequesterDomain;
		CString szDefaultRequesterName;
		CString szReturnEmailName;
		CString szReturnEmailAddress;
		CString szHoldNoticeEmailAddress;
		CString szEmailSubject;
		CString szEmailSubjectNotAvailable;
		CString szEmailSubjectPickupNotice;
		CString szEmailSubjectInfoLink;
		CString szEmailSubjectMoveILL;
		CString szEmailSubjectHold;
		CString szArticleAvailable;
		CString szArticleSomeAvailable;
		CString szArticleNoneAvailable;
		CString szNotAvailableMessage;
		CString szSendMessageFile;
		CString szSendMessageFileNA;
		CString szSendMessageFileNA1;
		CString szSendMessageFileNA2;
		CString szLinkMessageFile;
		CString szNoteMessageFile;
		CString szAttachMessageFile;
		CString szRemindMessageFile;
		CString szFTPMessageFile;
		CString szFTPManualFile;
		CString szAttachManualFile;
		CString szNonEDDMessageFile;
		CString szMoveILLMessageFile;
		CString szHoldMessageFile;
		CString szHoldNoticeFile;
		CString szAddMessageFile;
		CString szSMTPServer;
		INT nSMTPPort;
		CString szPOPServer;
		INT nPOPPort;
		CString szEmailUser;
		CString szEmailPassword;
		CString szFailureSubject;
		INT nMaxFailureCheck;
		CString szFailureEmailHead;
		CString szFailureEmailFoot;
		INT nAttachmentmaxBytes;
		INT nFileSizeWarningBytes;
		CString szWebLinkAvailableText;
		CString szWebLinkNoneAvailableText;
		BOOL bSendToAlternateEmail;
		BOOL bSendEmailOnAdd;
	} m_Email;

	// [UserInfo]
	struct USERINFO
	{
		CString szUserName;
		CString szPassword;
		CString szDomain;
	} m_UserInfo;

	// [FTPWEB]
	struct FTPWEB
	{
		INT nServerPort;
		CString szServerName;
		CString szFTPUID;
		CString szFTPPWD;
		CString szServerFolder;
		BOOL bShowStatus;
	} m_FTP_WEB;

	// [FTPILL]
	struct FTPILL
	{
		INT nServerPort;
		CString szServerName;
		CString szFTPUID;
		CString szFTPPWD;
		CString szServerFolder;
		BOOL bShowStatus;
	} m_FTP_ILL;

};

////////////////////////////////////////////////////////////////
// MSDN Magazine -- August 2003
//
// Modified for use with UNICODE 2010 by TeamSoftware Solutions
//
// ---
// This class encapsulates the process of finding a window with a given class name
// as a descendant of a given window. To use it, instantiate like so:
//
//		CFindWnd fw(hwndParent,classname);
//
// fw.m_hWnd will be the HWND of the desired window, if found.
//
class CFindWnd {
private:
	//////////////////
	// This private function is used with EnumChildWindows to find the child
	// with a given class name. Returns FALSE if found (to stop enumerating).
	//
	static BOOL CALLBACK FindChildClassHwnd(HWND hwndParent, LPARAM lParam) {
		CFindWnd *pfw = (CFindWnd*)lParam;
		HWND hwnd = FindWindowEx(hwndParent, NULL, pfw->m_classname, NULL);
		if (hwnd) {
			pfw->m_hWnd = hwnd;	// found: save it
			return FALSE;			// stop enumerating
		}
		EnumChildWindows(hwndParent, FindChildClassHwnd, lParam); // recurse
		return TRUE;				// keep looking
	}

public:
	LPCTSTR m_classname;			// class name to look for
	HWND m_hWnd;					// HWND if found

	// ctor does the work--just instantiate and go
	CFindWnd(HWND hwndParent, LPCTSTR classname)
		: m_hWnd(NULL), m_classname(classname)
	{
		FindChildClassHwnd(hwndParent, (LPARAM)this);
	}
};
